﻿using System;
using System.Collections.Generic;

using System.Text;

namespace 命题证明
{
    /// <summary>验证字符串是否合法</summary>
    class ValidateExpression
    {
        //如果返回null，就表示这个字符串合法   这个方法是不是太纠结了
        public static void CheckValidate( string expression )
        {
            Stack<int> bracket = new Stack<int>();
            char previous, next;
            expression = expression.Replace( " ", "" );
            if ( expression == "" )
                throw new ExpressionException( 0, "没有表达式" );

            for ( int i = 0; i < expression.Length; i++ )
            {
                if ( !MainChar.IsValidateChar( expression[i] ) )
                {
                    throw new ExpressionException( i, "存在不合法的字符" );
                }
                char index = expression[i];
                if ( i != expression.Length - 1 )
                {
                    next = expression[i + 1];
                    //if ( index == next && !MainChar.IsBracket( index ) )
                    //    throw new ExpressionException( i, "存在两个连续一样的字符" );
                    //if ( MainChar.IsOperator( index ) && MainChar.IsOperator( next ) && next != MainChar.Not )
                    //    throw new ExpressionException( i, "存在两个连续的操作符" );
                    if ( MainChar.IsConst( index ) && MainChar.IsConst( next ) )
                        throw new ExpressionException( i, "存在两个连续的常数" );
                    if ( MainChar.IsWord( index ) && MainChar.IsWord( next ) )
                        throw new ExpressionException( i, "存在两个连续的操作数" );
                }
                if ( index == '(' )
                {
                    bracket.Push( i );
                }
                else if ( index == ')' )
                {
                    if ( bracket.Count == 0 )
                    {
                        throw new ExpressionException( i, "括号不匹配" );
                    }
                    bracket.Pop();
                }
                else if ( MainChar.IsOperator( index ) )
                {
                    if ( i == expression.Length - 1 )
                    {
                        throw new ExpressionException( i, "操作符不能作为最后一个字符" );
                    }
                    next = expression[i + 1];
                    if ( index == MainChar.Not )
                    {
                        if ( next == ')' )
                        {
                            throw new ExpressionException( i, "\"" + MainChar.Not.ToString() + "\"" + " 操作符后面不能接\")\"" );
                        }
                        if ( i != 0 )
                        {
                            previous = expression[i - 1];
                            if ( previous == ')' || MainChar.IsWord( previous ) )
                            {
                                throw new ExpressionException( i - 1, "\"" + MainChar.Not.ToString() + "\"" + "操作符的前面不能接\"(\",或操作数" );
                            }
                        }
                    }
                    else
                    {
                        if ( i == 0 )
                        {
                            throw new ExpressionException( i, "双目操作符不能作为第一个字符" );
                        }
                        previous = expression[i - 1];
                        if ( !MainChar.IsWord( previous ) && previous != ')' && !MainChar.IsConst( previous ) )
                            throw new ExpressionException( i - 1, "双目操作符的前面必须接操作数,或\")\"" + ",或常数" );
                        if ( !MainChar.IsWord( next ) && next != '(' && next != MainChar.Not && !MainChar.IsConst( next ) )
                            throw new ExpressionException( i + 1, "双目操作符的后面必须接操作数,或\"(\",或\" " + MainChar.Not.ToString() + "\",或常数" );
                    }
                }
                else if ( MainChar.IsWord( index ) || MainChar.IsConst( index ) )
                {
                    if ( i != 0 )
                    {
                        previous = expression[i - 1];
                        if ( previous == ')' || MainChar.IsConst( previous ) )
                        {
                            throw new ExpressionException( i - 1, "操作数的前面不能是\")\",或操作数" );
                        }
                    }
                    else if ( i != expression.Length - 1 )
                    {
                        next = expression[i + 1];
                        if ( next == '(' || MainChar.IsConst( next ) )
                        {
                            throw new ExpressionException( i + 1, "操作数的后面不能是\"(\",或操作数" );
                        }
                    }

                }
            }


            if ( bracket.Count != 0 )
            {
                throw new ExpressionException( bracket.Pop(), "括号不匹配" );
            }

            //return null;
        }

    }
}
